package day3;

import java.io.*;

//测试序列化
public class TestSerializable {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //output();
        input();
    }

    //序列化
    public static void output() throws IOException {
        Student s = new Student();
        s.setName("wuxi");
        s.setSex("男");
        s.setScore(100);
        StudentInfo info = new StudentInfo();
        info.setName("你好");
        info.setSex("女");
        info.setPhone("126149579");
        s.setInfo(info);
        //因为序列化是转化成字节序列     所以文件内容不能修改
        //所以本地文件会设置一个   无法打开的后缀名

        FileOutputStream fos = new FileOutputStream("d://io//student.xxx");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(s);
    }

    //反序列化
    public static void input() throws IOException, ClassNotFoundException {
        FileInputStream fis = new FileInputStream("D://io//student.xxx");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Student stu = (Student) ois.readObject();
        System.out.println(stu.getName() + " " + stu.getSex() + " " + stu.getScore());
        System.out.println(stu.getInfo().getPhone());
    }
}

class Student implements Serializable {
    //序列化还会默认的唯一标识：作用用于检测序列化和反序列化是否是同一个ID
    private static final long serialVersionUID = 1234567812345678L;

    private String name;

    //忽略序列化 属性的值不保存
    private static String sex;
    //忽略序列化 属性的值不保存
    private transient double score;
    private StudentInfo info;

    public StudentInfo getInfo() {
        return info;
    }

    public void setInfo(StudentInfo info) {
        this.info = info;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
}

class StudentInfo implements Serializable {
    private String name;
    private String sex;
    private String phone;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}
